package cc.smarnet.caihou.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.io.Serializable;
import java.util.List;

import cc.smarnet.caihou.R;
import cc.smarnet.caihou.entity.Bracts;
import cc.smarnet.caihou.phone.bracts.BractsDetailActivity;

/**
* 毛产品信息的adapter
* */
public class BractsAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<Bracts> list;

    public BractsAdapter(Context context,List<Bracts> list){
        this.context=context;
        this.list=list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.bracts_item,parent,false);
        BractsViewHolder bractsViewHolder=new BractsViewHolder(view);
        return bractsViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Bracts bracts=list.get(position);
        BractsViewHolder bractsViewHolder= (BractsViewHolder) holder;
        bractsViewHolder.baseName.setText(bracts.getBaseName());
        bractsViewHolder.greenhouseName.setText(bracts.getGreenhouseName());
        bractsViewHolder.varietyDictText.setText(bracts.getVarietyDictText());
        bractsViewHolder.harvestCount.setText(String.valueOf(bracts.getHarvestCount()));
        bractsViewHolder.harvestUser.setText(bracts.getHarvestUser());
        bractsViewHolder.harvestWeight.setText(String.valueOf(bracts.getHarvestWeight()));
        bractsViewHolder.harvestTime.setText(bracts.getHarvestTime());
        bractsViewHolder.createTime.setText(bracts.getCreateTime());
    }

    @Override
    public int getItemCount() {
        // list.size()为0的话也必须至少返回1，返回有数值的话才会显示item，因为该方法不会返回0
        return list.size() > 0 ? list.size() : 1;
    }

    class BractsViewHolder extends RecyclerView.ViewHolder{
        TextView baseName;
        TextView greenhouseName;
        TextView varietyDictText;
        TextView harvestCount;
        TextView harvestUser;
        TextView harvestWeight;
        TextView harvestTime;
        TextView createTime;

        public BractsViewHolder(@NonNull View itemView) {
            super(itemView);
            baseName=itemView.findViewById(R.id.bracts_item_baseName);
            greenhouseName=itemView.findViewById(R.id.bracts_item_greenhouseName);
            varietyDictText=itemView.findViewById(R.id.bracts_item_varietyDictText);
            harvestCount=itemView.findViewById(R.id.bracts_item_harvestCount);
            harvestUser=itemView.findViewById(R.id.bracts_item_harvestUser);
            harvestWeight=itemView.findViewById(R.id.bracts_item_harvestWeight);
            harvestTime=itemView.findViewById(R.id.bracts_item_harvestTime);
            createTime=itemView.findViewById(R.id.bracts_item_createTime);

            // item点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bracts bracts=list.get(getAdapterPosition());
                    Intent intent = new Intent(context, BractsDetailActivity.class);
                    intent.putExtra("bractsDetail", (Serializable) bracts);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                    context.startActivity(intent);
                }
            });
        }
    }
}
